// pages/personaldetail/personaldetail.js
const url = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: [
      { name: '1', value: '男', checked: 'true' },
      { name: '0', value: '女' }
    ],
    imdid:0,
    baseUrl: '',
    user:{}
  },
  changeavt:function(){
      wx.chooseImage({
        success: (res)=> {
          var tempFilePaths = res.tempFilePaths  //图片
          var accesstoken = wx.getStorageSync('accesstoken')
          wx.uploadFile({
            url: 'https://api.tianlala.com.cn/images', //仅为示例，非真实的接口地址
            filePath: tempFilePaths[0],
            header: {
              'Authorization': accesstoken
            },
            name: 'image', //文件对应的参数名字(key)
            formData: {},  //其它的表单信息
            success:(res)=>{
              console.log(res)
              var info =JSON.parse(res.data)
              console.log(info.image)
              var avatar = 'user.avatar'
              this.setData({
                [avatar]: info.image,
                imdid:info.id
              })
            }
          })
        }
      })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var user = wx.getStorageSync('user')
    if (user.gender == '1'){ 
      this.setData({
        items: [
          { name: '1', value: '男', checked: 'true' },
          { name: '0', value: '女' }
        ],
      })
    } else if (user.gender == '0'){
      this.setData({
        items: [
          { name: '1', value: '男'},
          { name: '0', value: '女', checked: 'true' }
        ],
      })
    }
    this.setData({
      baseUrl: url.baseUrl,
      user:user
    })
    console.log(this.data.user)
  },
  radioChange(e) {
    var gender = 'user.gender'
    this.setData({
      [gender]: e.detail.value
    })
    // console.log('radio发生change事件，携带value值为：', e.detail.value)
  },
  nicheng(e) {
    // console.log('携带value值为：', e.detail.value)
    var username = 'user.username'
    this.setData({
      [username]: e.detail.value
    })
  },
  gexing(e) {
    // console.log('携带value值为：', e.detail.value)
    var signature = 'user.signature'
    this.setData({
      [signature]: e.detail.value
    })
  },
  realname(e) {
    // console.log('携带value值为：', e.detail.value)
    var real_name = 'user.real_name'
    this.setData({
      [real_name]: e.detail.value
    })
  },
  phone(e) {
    // console.log('携带value值为：', e.detail.value)
    var phone = 'user.phone';
    this.setData({
      [phone]: e.detail.value
    })
  },
  age(e) {
    // console.log('携带value值为：', e.detail.value)
    var age = 'user.age'
    this.setData({
      [age]: e.detail.value
    })
  },
  desc(e) {
    // console.log('携带value值为：', e.detail.value)
    var desc = 'user.description'
    this.setData({
      [desc]: e.detail.value
    })
    // console.log(this.data.user)
  },
  school(e) {
    // console.log('携带value值为：', e.detail.value)
    var school = 'user.school'
    this.setData({
      [school]: e.detail.value
    })
    console.log(this.data.user)
  },
  //提交
  submitinfo:function(){
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(this.data.user.phone)) {
      wx.showToast({
        title: '手机号有误！',
        icon: 'none',
        duration: 1500
      });
      return false;
    }
    wx.showLoading({
      title: '请稍等',
    })
    var accesstoken = wx.getStorageSync('accesstoken')
    wx.request({
      url: 'https://api.tianlala.com.cn/user', // 仅为示例，并非真实的接口地址
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': accesstoken
      },
      method:'PUT',
      data: {
        signature: this.data.user.signature,
        description: this.data.user.description,
        gender: this.data.user.gender,
        real_name: this.data.user.real_name,
        phone: this.data.user.phone,
        school: this.data.user.school,
        age: this.data.user.age,
        username: this.data.user.username,
        avatar:this.data.imdid
      },
      success: (res) => {
        console.log(res, '成功')
        wx.hideLoading();
        wx.setStorageSync('user', this.data.user)
        if (res.statusCode == 200){
          wx.showToast({
            title: '保存成功',
          })
          setTimeout(() => {
            wx.switchTab({
              url: '/pages/personal/personal',
            })
          }, 2000)
        }
      },
      fail: (res) => {
        console.log(res, '失败')
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})